#include "usermanagement.h"

bool UserManagementBase::Insert(const QJsonObject &userInfo) {
    QString username = userInfo["username"].isNull() ? "NULL" : QString("'%1'").arg(userInfo["username"].toString());
    QString password = userInfo["password"].isNull() ? "NULL" : QString("'%1'").arg(userInfo["password"].toString());
    int age = userInfo["age"].isNull() ? -1 : userInfo["age"].toInt();
    QString gender = userInfo["gender"].isNull() ? "NULL" : QString("'%1'").arg(userInfo["gender"].toString());
    QString phone = userInfo["phone"].isNull() ? "NULL" : QString("'%1'").arg(userInfo["phone"].toString());
    QString address = userInfo["address"].isNull() ? "NULL" : QString("'%1'").arg(userInfo["address"].toString());

    QString query = QString("INSERT INTO %1 (username, password, age, gender, phone, address) VALUES (%2, %3, %4, %5, %6, %7)")
            .arg(tb)  // 表名
            .arg(username)
            .arg(password)
            .arg(age)
            .arg(gender)
            .arg(phone)
            .arg(address);

    return sql_m->executeQuery(query);
}

bool UserManagementBase::Remove(const QString &userId) {
    QString query = QString("DELETE FROM " + tb + " WHERE id = %1").arg(userId);
    return sql_m->executeQuery(query);
}

bool UserManagementBase::Update(const QJsonObject &json_value) {
    QString userId = json_value["id"].toString();
    if (userId.isEmpty()) {
        LOG_DEBUG() << "userid is empty";
        return false; // 返回 false 表示更新失败
    }

    QString username = json_value["username"].toString();
    QString age = QString::number(json_value["age"].toInt());  // 整数转字符串
    QString gender = json_value["gender"].toString();
    QString phone = json_value["phone"].toString();
    QString address = json_value["address"].toString();

    QString query = QString("UPDATE %1 SET username = '%2', age = '%3', gender = '%4', phone = '%5', address = '%6' WHERE id = '%7'")
            .arg(tb)
            .arg(username)
            .arg(age)
            .arg(gender)
            .arg(phone)
            .arg(address)
            .arg(userId);

    return sql_m->executeQuery(query);
}

QString UserManagementBase::GetIdByName(const QString &username) {
    QString query = QString("SELECT id FROM " + tb + " WHERE username = '%1'").arg(username);
    QVariant result = sql_m->executeQuerySingleResult(query);

    if (result.isValid()) {
        return result.toString();
    }

    return "";
}

QVariantMap UserManagementBase::GetInfo(const QString &userId) {
    QString query = QString("SELECT username, password, age, gender, phone, address FROM " + tb + " WHERE id = '%1'").arg(userId);
    QVariantMap result = sql_m->executeQueryResult(query);

//    LOG_DEBUG() << "Query: " << query;
//    LOG_DEBUG() << "Result: " << result;

    return result;
}

bool UserManagementBase::isExist(const QString &userId) {
    QString query = QString("SELECT COUNT(*) FROM " + tb + " WHERE id = '%1'").arg(userId);
    QVariant result = sql_m->executeQuerySingleResult(query);

    return result.isValid() && result.toInt() > 0; // 用户存在
}

QVariantMap DoctorManagement::GetInfo(const QString &userId)
{
    QString query = QString("SELECT username, profession, password, age, gender, phone, address FROM " + tb + " WHERE id = '%1'").arg(userId);
    QVariantMap result = sql_m->executeQueryResult(query, "doctor");

//    LOG_DEBUG() << "Query: " << query;
//    LOG_DEBUG() << "Result: " << result;

    return result;
}


bool DoctorManagement::Update(const QJsonObject &json_value) {
    QString userId = json_value["id"].toString();
    if (userId.isEmpty()) {
        LOG_DEBUG() << "userid is empty";
        return false; // 返回 false 表示更新失败
    }

    QString username = json_value["username"].toString();
    QString age = QString::number(json_value["age"].toInt());  // 整数转字符串
    QString gender = json_value["gender"].toString();
    QString phone = json_value["phone"].toString();
    QString address = json_value["address"].toString();
    QString profession = json_value["profession"].toString();  // 新增 profession 项

    // 构造 SQL 更新查询，去掉 password 字段
    QString query = QString("UPDATE %1 SET username = '%2', age = '%3', gender = '%4', phone = '%5', address = '%6', profession = '%7' WHERE id = '%8'")
            .arg(tb)
            .arg(username)
            .arg(age)
            .arg(gender)
            .arg(phone)
            .arg(address)
            .arg(profession)
            .arg(userId);

    return sql_m->executeQuery(query);
}

// 更新医生下拉框
QVariantList DoctorManagement::GetDoctorsByProfession(const QString &profession) {
    QString query = QString("SELECT username FROM %1 WHERE profession = ?").arg(tb);

    // 准备参数化查询
    QVariantList result = sql_m->executeQueryResultList(query, profession);

//    LOG_DEBUG() << "query: " << query;
//    LOG_DEBUG() << "result: " << result;

    return result;
}

QVariantMap DoctorManagement::GetAllInfos() {
    QString query = QString("SELECT username, profession, age, gender, phone, address FROM ") + tb; // 更新后的 SQL 查询
    QVariantList queryResult = sql_m->executeQueryResultList(query, QueryType::DoctorDetails);

    QVariantMap result;
    for (const auto &record : queryResult) {
        const QVariantMap &doctorInfo = record.toMap(); // 确保正确处理

        // 使用医生的用户名作为键，将信息存储到 result 中
        result[doctorInfo["username"].toString()] = doctorInfo;
    }

    return result;
}
